//
//  HMTabBarController.swift
//  WeiBoSwift
//
//  Created by hgj on 2016/11/16.
//  Copyright © 2016年 HuGuoJian. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupTabBar()
        
        addChildViewControllers()
    }
    
    private func setupTabBar() {
        
        // 实例化
        let tabBar = HMTabBar()
        
        // 只读属性不能够直接赋值  --> 间接赋值  kvc
        setValue(tabBar, forKey: "tabBar")
        
        // =======解决循环引用=======
        tabBar.composeClosure = { [weak self] in
            print(self!.view)
            
            let composeView = HMComposeView()
            composeView.show(fromVC: self!)
//            UIApplication.shared.keyWindow?.addSubview(composeView)
        }
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(vc: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home", index: 0)
        addChildViewController(vc: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(vc: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover", index: 2)
        addChildViewController(vc: HMProfileTableViewController(), title: "我", imageName: "tabbar_profile", index: 3)
    }
    
    // 重载 或者 自定义方法 都可以
    private func addChildViewController(vc: UIViewController, title: String, imageName: String, index: Int) {
        
        // 文字
        vc.title = title
        
        vc.tabBarItem.tag = index
        
        /*
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        */
        
        // 图片 以及 渲染方式
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        // 标签文字属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: .normal)
        
        // 文字位置/偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3) 
        
        /* 设置图片的偏移
        vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        */
 
        /* 设置badge
        vc.tabBarItem.badgeColor = UIColor.red
        vc.tabBarItem.badgeValue = 10
    vc.tabBarItem.setBadgeTextAttributes(<#T##textAttributes: [String : Any]?##[String : Any]?#>, for: <#T##UIControlState#>)
        */
        
        let navController  = HMNavController(rootViewController: vc)
        
        addChildViewController(navController)
    }
    
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        //1.应该获取到被点击的UITabBarButton
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //可以获取到被点击tabbarButton
                    //遍历子视图
                    for v in subView.subviews {
                        
                        // "UITabBarSwappableImageView" 替换成 UIImageView 也可以
                        if v.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            //线缩小
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                               
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
}
